const app = getApp();
Page({
  data: {
    hotList: [],
    newList: [],
    bannerList: [],
    allCate:[],
  },
  changeIndicatorDots(e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  storageList(){
    var that = this;
    my.getStorage({
      key: 'hotList',
      success: function(res) {
        if(typeof res.data=='undefined' || res.data!=null ){
          that.setData({
            hotList:res.data.hotList
          })
        }
      }
    });
    my.getStorage({
      key: 'newList',
      success: function(res) {
        if(typeof res.data=='undefined' || res.data!=null ){
          that.setData({
            newList:res.data.newList
          })
        }
      }
    });
    my.getStorage({
      key: 'bannerList',
      success: function(res) {
        if(typeof res.data=='undefined' || res.data!=null ){
          that.setData({
            bannerList:res.data.bannerList
          })
        }
      }
    });
    my.getStorage({
      key: 'allCate',
      success: function(res) {
        if(typeof res.data=='undefined' || res.data!=null ){
          that.setData({
            allCate:res.data.allCate
          })
        }
      }
    });
  },
  onPullDownRefresh() {
    this.indexList();
    my.stopPullDownRefresh();
  },
  onLoad() {
   //my.clearStorage();
   // app.checkTokenLogin();
   //my.clearStorageSync();
   var that = this;
   that.indexList();
  },
  indexList(){
    var that = this;
    my.httpRequest({
      url: app.geturl+'/banner/list',
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        that.setData({
          bannerList: res.data.data.list
        });
        my.setStorage({
          key: 'bannerList',
          data: {
            bannerList:res.data.data.list
          },
          success: function() {
            
          }
        });
      }
    });
    my.httpRequest({
      url: app.geturl+'/goods-common/list',
      method: 'GET',
      data: {
        is_recommend: 1,
        is_hot:1
      },
      dataType: 'json',
      success: function(res) {
        that.setData({
          hotList: res.data.data.list
        });
        my.setStorage({
          key: 'hotList',
          data: {
            hotList:res.data.data.list
          },
          success: function() {
            
          }
        });
      }
    });
    my.httpRequest({
      url: app.geturl+'/goods-class/list',
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        var num = res.data.data.list.length;
        var arr = new Object();
        for(var i=0;i<num;i++){
          if(i<5){
            arr[i] = res.data.data.list[i];
          }
        }
        my.setStorage({
          key: 'allCate',
          data: {
            allCate:arr
          },
          success: function() {
            
          }
        });
        that.setData({
          allCate:arr
        });
      }
    });
    my.httpRequest({
      url: app.geturl+'/goods-common/list',
      method: 'GET',
      data: {
        is_new: 1,
      },
      dataType: 'json',
      success: function(res) {
        that.setData({
          newList: res.data.data.list
        });
        my.setStorage({
          key: 'newList',
          data: {
            newList:res.data.data.list
          },
          success: function() {
            
          }
        });
      }
    });
  },
  cateList(e){
    var id = e.target.dataset.id;
    my.navigateTo({ url: '../add-todo/all?id='+id });
  },
  handleTap(){
    my.navigateTo({ url: '../add-todo/all' });
  },
  goodsDetail(e){
    var id = e.target.dataset.id;
    my.navigateTo({ url: '../add-todo/detail?id='+id });
  }
});